package cn.edu.tust.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.edu.tust.bean.UserInfoBean;
import cn.edu.tust.model.UserInfoModel;
import cn.edu.tust.myUtils.MyUtils;

/**
 * 查询操作
 */
@WebServlet("/UserInfoQueryServlet")
public class UserInfoQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 进行数据查询操作
		// 设置request、response的编码，防止中文乱码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 获取表单数据
		System.out.println("用户信息查询...");
		// 获取用户名
		String strUserName = request.getParameter("txtUserName");
		// 获取密码
		String strUserPsw = request.getParameter("txtUserPsw");
		// 获取真实姓名
		String strUserRealName = request.getParameter("txtUserRealName");
		// 获取邮箱
		String strUserEmail = request.getParameter("txtUserEmail");
		// 获取查询开始时间
		String strUserRegisterDate = request.getParameter("txtUserRegisterDate");
		// 获取查询结束时间
				String strUserRegisterDate2 = request.getParameter("txtUserRegisterDate2");

		// 将获取的表单的数据信息保存的实体类中
		// UserInfoBean userInfoB = new UserInfoBean();
		UserInfoBean userInfoB = MyUtils.getNewInstance(UserInfoBean.class);
		// 存入获取的表单的用户名
		userInfoB.setUserName(strUserName);
		// 存入获取的表单的用户名
		userInfoB.setUserPsw(strUserPsw);
		userInfoB.setUserRealName(strUserRealName);
		userInfoB.setUserEmail(strUserEmail);
		userInfoB.setUserRegisterDate(MyUtils.convertString2Date(strUserRegisterDate,"yyyy-MM-dd"));
		userInfoB.setUserRegisterDate2(MyUtils.convertString2Date(strUserRegisterDate2,"yyyy-MM-dd"));
		
		// 进行ajax响应处理
		// 调用model层进行数据库查询操作
		List<UserInfoBean> lstUserInfo = MyUtils.getNewInstance(UserInfoModel.class).getUserInfoByParams(userInfoB);
		// 将查询到的结果集转换为json字符串
		String strResult = JSON.toJSONStringWithDateFormat(lstUserInfo, "yyyy-MM-dd HH:mm:ss");
		// 将查询结果返回给ajax引擎
		PrintWriter pw = response.getWriter();
		// 将返回的内容发送给客户端的ajax引擎
		pw.println(strResult);
	}
}
